﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Processing;
using System.Drawing;
using Processing.Structures;
using System.Runtime.InteropServices;

namespace StrideSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Log";

            Bitmap bmp = new Bitmap(255 * 8, 255 * 5, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            Console.Write("Ausführen der Pseudo SumColor Kernel .");
            General.TimeClock.Start("Kernel");
            Processor processor = new Processor(Cloo.ComputePlatform.Platforms[0], Cloo.ComputePlatform.Platforms[0].Devices[0]);
            processor.DebugMode = true;
            NeKernel nk = new NeKernel(processor);
            nk.colors = processor.AllocateBuffer<Byte4>(bmp.Height * bmp.Width);
            nk.Run();
            Byte4[] data = nk.colors.Data;
            Console.Write("\n");
            General.TimeClock.Stop("Kernel");

            Console.WriteLine("");

            Console.WriteLine("Kopieren der einzelenen Pixel und Korrektur von BGRA zu ARGB ..");
            General.TimeClock.Start("SingleCopy");
            for (int i = 0; i < data.Length; i++) bmp.SetPixel(i % bmp.Width, i / bmp.Width, Color.FromArgb(data[i].s0, data[i].s1, data[i].s2, data[i].s3));
            General.TimeClock.Stop("SingleCopy");

            Form1 f1 = new Form1(bmp);
            f1.Show();

            Console.WriteLine("");

            bmp = new Bitmap(255 * 8, 255 * 5, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Console.WriteLine("Kopieren der Adresse aber ohne Korrektur in BGRA ..");
            General.TimeClock.Start("MarshalCopy");
            GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);
            byte[] xdata = new byte[data.Length * 4];
            Marshal.Copy(hData.AddrOfPinnedObject(), xdata, 0, data.Length * 4);
            hData.Free();

            Size size = bmp.Size;
            System.Drawing.Imaging.BitmapData bdata = bmp.LockBits(
                new Rectangle(Point.Empty, size),
                System.Drawing.Imaging.ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            Marshal.Copy(xdata, 0, bdata.Scan0, data.Length * 4);
            xdata = null;
            bmp.UnlockBits(bdata);
            General.TimeClock.Stop("MarshalCopy");

            nk.colors.Dispose();
            GC.Collect();

    

            Console.WriteLine("");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Letztere Methode ist 100 fach performanter, erfordert aber alle Farben in BRGA!");

            (new Form1(bmp)).ShowDialog();

            Console.ReadKey();
        }
    }
}
